package com.shujia;

import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Code8InserData2 {
    static Connection connection;

    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileReader("source/jdbc.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Class.forName(properties.getProperty("driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            connection = DriverManager.getConnection(
                    properties.getProperty("url"),
                    properties.getProperty("user"),
                    properties.getProperty("password")
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 优化插入方式
     *
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {
        Statement statement = connection.createStatement();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            String insertSql = "INSERT INTO useinfo values ";
            for (int j = 0; j < 100; j++) {
                String value = "('" + j + "','zhangsan'," + "'zhangsan@163.com'," + "'18'" + "),";
                insertSql += value;
            }
            System.out.println(insertSql);
            statement.executeUpdate(insertSql.substring(0,insertSql.length()-1));
        }
        long end = System.currentTimeMillis();
        statement.close();
        connection.close();
        System.out.println("执行插入时间为："+(end-start)/1000+"秒");
    }
}
